package com.tw.ddd.trainning.umlhomework.parking;

public class SpaceParkingStrategy implements ParkingStrategy {
    @Override
    public ParkingLot chooseParkingLot(ParkingLot[] parkingLots) {
        if (parkingLots == null) {
            return null;
        }

        ParkingLot nextAvailableParkingLot = null;

        for (int i = 0; i < parkingLots.length; i++) {
            if (nextAvailableParkingLot == null || nextAvailableParkingLot.getAvailableNumber() < parkingLots[i].getAvailableNumber()) {
                nextAvailableParkingLot = parkingLots[i];
            }
        }

        if(nextAvailableParkingLot != null && !nextAvailableParkingLot.isAvailable()){
            nextAvailableParkingLot = null;
        }
        return nextAvailableParkingLot;
    }
}
